package com.sj.manager.listener;

import com.sj.manager.config.App;
import com.sj.manager.config.WebMvcConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @desc:
 * @author: xsj
 * @date: 2022/3/10 11:13
 */
public class WebInitializer implements WebApplicationInitializer{

    /**
     * 启动时候加载注册相关配置
     * @param servletContext
     * @throws ServletException
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException{
        //基于注解的配置
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        //注册定义的数据库相关配置文件,web相关的配置
        applicationContext.register(App.class, WebMvcConfig.class);
        //设置servlet
        applicationContext.setServletContext(servletContext);
        //配置spring mvc控制器
        ServletRegistration.Dynamic dynamic = servletContext.addServlet("springmvc",new DispatcherServlet(applicationContext));
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);
    }
}
